package com.luhuiguo.gwt.mvp.dispatcher.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.luhuiguo.gwt.mvp.dispatcher.shared.rpc.Action;
import com.luhuiguo.gwt.mvp.dispatcher.shared.rpc.Result;

/**
 * An abstract base class that provides methods that can be called to handle
 * success or failure results from the remote service. These should be called by
 * the implementation of
 * {@link #execute(com.luhuiguo.gwt.mvp.dispatcher.shared.rpc.Action, com.google.gwt.user.client.rpc.AsyncCallback)}
 * .
 * 
 * @author LuHuiguo
 * @version $Id: AbstractDispatcherAsync.java 11 2009-12-26 13:39:17Z luhuiguo $
 */
public abstract class AbstractDispatcherAsync implements DispatcherAsync {

    private final ExceptionHandler exceptionHandler;

    public AbstractDispatcherAsync(ExceptionHandler exceptionHandler) {
        this.exceptionHandler = exceptionHandler;
    }

    protected <A extends Action<R>, R extends Result> void onFailure(A action, Throwable caught,
            final AsyncCallback<R> callback) {
        if (exceptionHandler != null && exceptionHandler.onFailure(caught) == ExceptionHandler.Status.STOP) {
            return;
        }

        callback.onFailure(caught);
    }

    protected <A extends Action<R>, R extends Result> void onSuccess(A action, R result, final AsyncCallback<R> callback) {
        callback.onSuccess(result);
    }

}
